<?php 

include 'config.php';
include 'db_connection.php';

$area = $_REQUEST['patients_array'];
// $status = $_REQUEST['status'];

if(isset($_POST['Insert']) && $area != null){
  
  $filename = "./files/temp";
  $fd = fopen($filename, "w+");

  //fwrite($fd, utf8_encode($area) );
  fwrite($fd, $area );
  $file_contents = file_get_contents($filename);
//   echo $area;  

  fclose($fd);
  $fp = fopen( $filename, "r" );

  while ( !feof ( $fp) ) {

    $buffer = stream_get_line( $fp, 1024, "\n" );

    $patients = explode("\t", $buffer);

    $query = mysql_query("SELECT pass_num, dob, pat_id, unit, outpatient_of FROM PATIENTS WHERE pass_num = '$patients[2]' AND dob = '$patients[3]' ");
    $query2 = mysql_query("SELECT pat_id, med_unit, outpatient_of FROM HISTORY WHERE pass_num = '$patients[2]'");
    $count = mysql_num_rows($query);
    $count2 = mysql_num_rows($query2);
    $pin = mysql_fetch_row($query);
    $pin2 = mysql_fetch_row($query2);
    $pass_num = $pin[0];
    $dob = $pin[1];
    $pat_id = $pin[2];
    $unit = $pin[3];
    $outpatient_of = $pin[4];
    $date_of_change = date("j/n/Y");

    if ($count == 0) {
      $query3 = mysql_query("INSERT INTO PATIENTS (
	name,
	pass_num,
	dob,
	unit,
	location,
	outpatient_of,
	status,
	date_of_flight,
	visa_expiration,
	departed,
	notes) 
	VALUES ( 
	'$patients[1]', 
	'$patients[2]',
	'$patients[3]',
	'$patients[4]',
	'$patients[5]',
	'$patients[6]',
	'$patients[7]',
	'$patients[8]',
	'$patients[9]',
	'$patients[10]',
	'$patients[11]')");

	if (!$query3) {
	  die('Invalid INSERT query: ' . mysql_error());
	}
	// if a new patient is being inserted into PATIENTS
	// this query will provide with the newly auto-created pat_id
	$query10 = mysql_query("SELECT pat_id FROM PATIENTS WHERE pass_num = '$patients[2]' ");
	$arr = mysql_fetch_row($query10);
	$patID = $arr[0];
      // This query is used to insert entries in the HISTORY table
      // to keep track of a patient's history
      $query4 = mysql_query("INSERT INTO HISTORY (
	pat_id,
	pass_num,
	date_of_change,
	med_unit,
	outpatient_of )
      VALUES (
	'$patID', 
	'$patients[2]', 
	'$date_of_change', 
	'$patients[4]',
	'$patients[6]'
      )");
      if (!$query4) {
	die('Invalid INSERT query: ' . mysql_error());
      }

	$buffer = '';
	//echo $player_name;
   }//end of if

  else {
     if ($count2 == 0) {
      // This query is used to insert entries in the HISTORY table
      // to keep track of a patient's history
	  $query6 = mysql_query("INSERT INTO HISTORY (
	    pat_id,
	    pass_num,
	    date_of_change,
	    med_unit,
	    outpatient_of )
	  VALUES (
	    '$pat_id', 
	    '$patients[2]', 
	    '$date_of_change', 
	    '$patients[4]',
	    '$patients[6]'
	  )");
	  if (!$query6) {
	    die('Invalid INSERT query: ' . mysql_error());
	  }
     }// end of if
    else {
      // This query is used to insert entries in the HISTORY table
      // to keep track of a patient's history
// echo "$pin2[0] - $pat_id";
// echo "<br>";
// echo "$pin2[1] - $patients[4]";
// echo "<br>";
// echo "$pin2[2] - $patients[6]";
// echo "<br>";

      if (($pin2[0] == $pat_id) && ($unit != $patients[4] || $outpatient_of != $patients[6]) ) {
      $query7 = mysql_query("INSERT INTO HISTORY (
	    pat_id,
	    pass_num,
	    date_of_change,
	    med_unit,
	    outpatient_of )
	  VALUES (
	    '$pat_id', 
	    '$patients[2]', 
	    '$date_of_change', 
	    '$patients[4]',
	    '$patients[6]'
	  )");
	  if (!$query7) {
	    die('Invalid INSERT query: ' . mysql_error());
	  }
      } // end of inner if
    }

    $query8 = mysql_query("UPDATE PATIENTS SET
			    name = '$patients[1]',
			    pass_num = '$patients[2]',
			    dob = '$patients[3]',
			    unit = '$patients[4]',
			    location = '$patients[5]',
			    outpatient_of = '$patients[6]',
			    status = '$patients[7]',
			    date_of_flight = '$patients[8]',
			    visa_expiration = '$patients[9]',
			    departed = '$patients[10]'  
			  WHERE
			    pat_id = '$pat_id';");
     
      if (!$query8) {
	die('Invalid UPDATE query: ' . mysql_error());
      }
      $buffer = '';
    }//end of else

}//end of while

fclose($fp);

header("location:admin2.php");

}//end of if

else {
  echo "You didn't paste any text on the text area. However, if this message appears, well something isn't working as it should be..";
  header("location:admin.php");
}

?>
